/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODECPP_FOUNDATION_THREAD_WIN32_H_
#define NODECPP_FOUNDATION_THREAD_WIN32_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <process.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <NodeCpp/Event.h>

namespace NodeCpp
{
	class Runnable;

	class ThreadImpl
	{
	public:
		enum Priority
		{
			PRIO_LOWEST_IMPL = THREAD_PRIORITY_LOWEST,
			PRIO_LOW_IMPL = THREAD_PRIORITY_BELOW_NORMAL,
			PRIO_NORMAL_IMPL = THREAD_PRIORITY_NORMAL,
			PRIO_HIGH_IMPL = THREAD_PRIORITY_ABOVE_NORMAL,
			PRIO_HIGHEST_IMPL = THREAD_PRIORITY_HIGHEST
		};
		typedef DWORD   ID;

		ThreadImpl(void);
		~ThreadImpl(void);

		bool StartImpl(Runnable*);
		void JoinImpl(void);
		bool JoinableImpl(void);
		bool SetPriorityImpl(int);
		Priority GetPriorityImpl(void) const { return Priority_; }
		ID GetIDImpl(void) const { return ID_; }

		static ID GetCurrentIDImpl(void);
		static void SleepImpl(long);

	private:
		static unsigned int __stdcall _Proc(void*);

		ID ID_;
		uintptr_t Handle_;
		Runnable* Runnable_;
		Priority Priority_;
        Event StartEvent_;
	};

    class ThisThreadImpl
    {
    protected:
        ThisThreadImpl(void){}
        ~ThisThreadImpl(void){}

    public:
        static ThreadImpl::ID GetIDImpl(void);
        static void SleepImpl(long);
    };
}

#endif // NODECPP_FOUNDATION_THREAD_WIN32_H_
